//
//  BCHomePageViewController.m
//  BCVersion
//
//  Created by 吴 崧 on 14-2-23.
//  Copyright (c) 2014年 Jungle. All rights reserved.
//

#import "BCHomePageViewController.h"
#import "BCLoginViewController.h"
#import "UIViewController+EndWhenTouch.h"
#import "BCHomepageSearchTableViewController.h"
#import "ManagerSearchedModel.h"
@interface BCHomePageViewController ()<UITextFieldDelegate>

@end

@implementation BCHomePageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController.navigationBar setHidden:YES];
    self.searchConditionTF.delegate = self;
    self.scrollView.contentSize = CGSizeMake(320, 1000);
    [self addFunctionToEndEdit];
    // Do any additional setup after loading the view from its nib.
}





-(void)checkStatus:(BOOL)loged
           WithBtn:(UIButton *)btn
{

    NSString *title = nil;
    if (loged) {
        title = @"注销";
    }else{
        title = @"登录";
    }
    [btn setTitle:title forState:UIControlStateNormal];
}


- (IBAction)logInOrOut:(UIButton *)sender {
    if (USER_MANAGER.didLogin) {
        USER_MANAGER.auth = nil;
        [self checkStatus:USER_MANAGER.didLogin WithBtn:self.logStatusBtn];
    }else
    {
        [BCLoginViewController show];
    }
}


- (IBAction)startingSearch:(UIButton *)sender {
    [self.view endEditing:YES];
    __weak typeof(self)weakSelf = self;
    __block NSString *searchString = self.searchConditionTF.text.length == 0 ?EMPTY_STRING:self.searchConditionTF.text;
    //May add cover
    if (searchString.length == 0) {
        [UIAlertView showWithTitle:nil
                           message:@"请输入关键字"
                 cancelButtonTitle:@"确定"
                 otherButtonTitles:nil
                          tapBlock:nil];
    }else
    {
    [BCProgressHUD showWithTile:@"搜索中" animated:YES];
        __block NSArray *bankArray = nil;
        __block NSArray *workArray = nil;
        [BCNetworkClient banksSuccess:^(NSArray *banks) {
            bankArray = banks;
            [BCNetworkClient worksSuccess:^(NSArray *works) {
                workArray = works;
                [BCNetworkClient searchWithKey:searchString
                                          auth:USER_MANAGER.auth
                                          city:@"beijing"
                                          work:EMPTY_STRING
                                          bank:EMPTY_STRING
                                         order:@"score"
                                          desc:@"1"
                                         start:0
                                         limit:10
                                       success:^(NSArray *results) {
                                           [BCProgressHUD hideAnimated:YES];
                                           NSArray *result = [ManagerSearchedModel modelsWithDataSource:results];
                                           
                                           BCHomepageSearchTableViewController *table = [[BCHomepageSearchTableViewController alloc]initWithDataSource:result
                                                                                                                                            WorkSource:workArray
                                                                                                                                            BankSource:bankArray
                                                                                                                                            CellHeight:75
                                                                                                                                          FooterHeight:0
                                                                                                                                          HeaderHeight:0
                                                                                                                                               KeyWord:searchString];
                                           
                                           [weakSelf.navigationController pushViewController:table animated:YES];

                                       } failure:^(NSError *error) {
                                           [BCProgressHUD hideWithError:error animated:YES];
                                       }];
            } failure:^(NSError *error) {
            [BCProgressHUD hideWithError:error animated:YES];
            }];
        } failure:^(NSError *error) {
            [BCProgressHUD hideWithError:error animated:YES];
        }];
   }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self checkStatus:USER_MANAGER.didLogin WithBtn:self.logStatusBtn];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}


-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}





- (void)keyboardWillShow:(NSNotification *)aNotification
{
    NSDictionary *userInfo = [aNotification userInfo];
    
    
	CGRect keyboardFrameBefore = CGRectApplyAffineTransform([userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue], CGAffineTransformInvert(APP_Delegate.window.rootViewController.view.transform));
    
	CGRect keyboardFrameAfter = CGRectApplyAffineTransform([userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue], CGAffineTransformInvert(APP_Delegate.window.rootViewController.view.transform));
    
    CGFloat dHeight = 0;
    dHeight = keyboardFrameBefore.origin.y - keyboardFrameAfter.origin.y;
    
	if (dHeight != 0) {
        //CGFloat posY = self.view.frame.origin.y - dHeight;
    CGRect frame = CGRectSetY(self.view.frame, -100);
		
		[UIView animateWithDuration:[userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]
		                      delay:0
		                    options:[userInfo[UIKeyboardAnimationCurveUserInfoKey] intValue]
		                 animations: ^(void) {
                             self.view.frame = frame;
                         }
		                 completion: ^(BOOL finished) {
                             
                         }
         ];
	}
    
}

- (void)keyboardWillHide:(NSNotification *)aNotification
{
	NSDictionary *userInfo = [aNotification userInfo];
    
    
	CGRect keyboardFrameBefore = CGRectApplyAffineTransform([userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue], CGAffineTransformInvert(APP_Delegate.window.rootViewController.view.transform));
    
	CGRect keyboardFrameAfter = CGRectApplyAffineTransform([userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue], CGAffineTransformInvert(APP_Delegate.window.rootViewController.view.transform));
    
	CGFloat dHeight = 0;
    dHeight = keyboardFrameBefore.origin.y - keyboardFrameAfter.origin.y;
    if (dHeight != 0) {
       // CGFloat posY = self.view.frame.origin.y - dHeight;
        CGRect frame = CGRectSetY(self.view.frame, 0);

		
		[UIView animateWithDuration:[userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]
		                      delay:0
		                    options:[userInfo[UIKeyboardAnimationCurveUserInfoKey] intValue]
		                 animations: ^(void) {
                             self.view.frame = frame;
                         }
		                 completion: ^(BOOL finished) {
                             
                         }
         ];
	}
}



@end
